<?php
Prado::using('Application.common.common');
class EditProfile extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = "My Profile";
			$this->ProfileMenu->lnkEditProfile->CssClass = "active";
			
			if ($this->Request->contains('m')) {
				$this->Notice->Type = UserNoticeType::NoticeAdmin;
				if ($this->Request['m'] == 'uu')	
					$this->Notice->Text = $this->Application->getModule('message')->translate('UPDATE_SUCCESS','Profile','');
				
			}
			
			// Populate Age Group List
			$this->cboAgeSelector->DataSource = AgeGroupRecord::finder()->getAllItems();
			$this->cboAgeSelector->DataBind();
			
			// Populate Cuisine List
			$this->cboCuisineSelector->DataSource = CuisineRecord::finder()->getAllItems();
			$this->cboCuisineSelector->DataBind();
			
			$this->txtUserID->Value = $this->Application->User->ID;
			$userRecord = UserRecord::finder()->findByPk($this->txtUserID->Value);
			if ($userRecord instanceof UserRecord)
			{
				$this->txtUserName->Text=$userRecord->UserName;
				$this->txtEmail->Text=$userRecord->Email;
				
				$this->cboAgeSelector->SelectedValue = $userRecord->AgeGroup;	
				$this->cboCuisineSelector->SelectedValue = $userRecord->PreferredCuisine;	
				if ($userRecord->ReceiveUpdates == 'Y')
					$this->chkUpdates->Checked = true;				
			}
		}
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{						
			try {					
				$userRecord = UserRecord::finder()->findByPk($this->txtUserID->Value);
				$userRecord->UserName = $this->txtUserName->SafeText;
				$userRecord->Email = $this->txtEmail->SafeText;	
				$userRecord->AgeGroup = $this->cboAgeSelector->SelectedValue;
				$userRecord->PreferredCuisine = $this->cboCuisineSelector->SelectedValue;
				$userRecord->ReceiveUpdates = "N";
				if ($this->chkUpdates->Checked)	
					$userRecord->ReceiveUpdates = "Y";
				$userRecord->ModifyBy = $this->Application->User->ID;
				$userRecord->ModifyDate = time();										
				$userRecord->save();						
				$this->Response->redirect($this->Service->ConstructUrl('profile.EditProfile', array('m'=>'uu')));			
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
	}
	
	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("Index"));				
	}
}

?>